from MarketData.const import CONST
from Strategy.config import STATIC

#找到买价最高的平台，遍历所有平台的买入价格
def bestbid(platform, pairquote, commodity_cur, base_cur, account, minsize):
    platf_bid = platform[0]
    for platf in platform:
        #skip if not enough size
        #当前平台没有该币
        if commodity_cur not in account[platf].balance:
            continue;

        # 看有没有足够交易数量，对比，当前平台的，当前该币的要交易的买入数量、我们帐户该币的持有量(因为，我们要把币卖掉)、该币的最小交易量
        if min(pairquote[platf].volume_bid, account[platf].balance[commodity_cur]) < minsize:
            continue
        # 通过实际可以成交的价格（已经减去了平台手续费），找到最大交易价格的平台，从而我们可以卖出给对方。
        if pairquote[platf].price_bid * (1 - CONST.fee[platf]) > pairquote[platf_bid].price_bid * (1 - CONST.fee[platf_bid]):
            platf_bid = platf
    return platf_bid

#找到卖价最低的平台，遍历所有平台的买入价格
def bestask(platform, pairquote, commodity_cur, base_cur, account, minsize):
    platf_ask = platform[0]
    for platf in platform:
        #skip if not enough size
        # 当前平台没有该币
        if base_cur not in account[platf].balance:
            continue
        # 看有没有足够交易数量，对比，当前平台，当前该币的交易量、该币最小交易量；当前平台我们帐户base_cur的数量(用来买commodity)、 最小成交量＊成交价格(即最小需要的base_cur)
        if pairquote[platf].volume_ask < minsize or account[platf].balance[base_cur] < minsize * pairquote[platf].price_ask * 0.9:
            continue
        # 通过实际可以成交的价格（已经减去了平台手续费），找到最小交易价格的平台，从而我们可以买入。
        if pairquote[platf].price_ask * (1 + CONST.fee[platf]) < pairquote[platf_ask].price_ask * (1 + CONST.fee[platf_ask]):
            platf_ask = platf
    return platf_ask


def evaluation_mid(platform, pairquote, commodity_cur, base_cur, mid_cur, account, TS_profit):
    #ask price > bid price

    # 最小下单量
    minsize = CONST.minsize[commodity_cur]

    #select platform with best bid, and platform with best ask
    #找到commodity出买价最高的的平台
    platf_bid = bestbid(platform, pairquote, commodity_cur, base_cur, mid_cur, account, minsize)
    totalprice_bid = pairquote[platf_bid].price_bid * (1 - CONST.fee[platf_bid])
    #找到commodity出卖价最低的平台
    platf_ask = bestask(platform, pairquote, commodity_cur, base_cur, account, minsize)
    totalprice_ask = pairquote[platf_ask].price_ask * (1 + CONST.fee[platf_ask])

    #return (platf_bid, platf_ask) if qualified
    platf_order = dict()

    profit = totalprice_bid / totalprice_ask - 1
    if profit > TS_profit:
        platf_order["bid"] = platf_bid
        platf_order["ask"] = platf_ask
    return (profit, platf_order)


def evaluation(platform, pairquote, commodity_cur, base_cur, account, TS_profit):
    #ask price > bid price

    # 最小下单量
    minsize = CONST.minsize[commodity_cur]

    #select platform with best bid, and platform with best ask
    #找到commodity出买价最高的的平台
    platf_bid = bestbid(platform, pairquote, commodity_cur, base_cur, account, minsize)
    totalprice_bid = pairquote[platf_bid].price_bid * (1 - CONST.fee[platf_bid])
    #找到commodity出卖价最低的平台
    platf_ask = bestask(platform, pairquote, commodity_cur, base_cur, account, minsize)
    totalprice_ask = pairquote[platf_ask].price_ask * (1 + CONST.fee[platf_ask])

    #return (platf_bid, platf_ask) if qualified
    platf_order = dict()

    profit = totalprice_bid / totalprice_ask - 1
    if profit > TS_profit:  
        platf_order["bid"] = platf_bid
        platf_order["ask"] = platf_ask
    return (profit, platf_order)

def filter_Profit(profit, 
                  TS_profit_balance, platform, commodity, account, platf_order):
    if not platf_order:
        return

    sum_commodity = 0
    # 把所有平台的当前commdity数量相加
    for platf in platform:
        sum_commodity += account[platf].balance[commodity]

    # 最好的要价平台的commdity数量 ／ 所有平台的当前commdity数量总和
    prop_bid = account[platf_order["bid"]].balance[commodity] / max(sum_commodity,0.001)

    threshold = max(TS_profit_balance*(1-2*prop_bid), 0)
    if profit<threshold:
        platf_order.clear()
